package yuejian_07.yuejian_07.activity;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import yuejian_07.yuejian_07.R;
import yuejian_07.yuejian_07.base.RetrofitListener;
import yuejian_07.yuejian_07.base.RetrofitListenerTow;
import yuejian_07.yuejian_07.javabean.ActivityOrder;
import yuejian_07.yuejian_07.javabean.User;
import yuejian_07.yuejian_07.model.ActivityModel;
import yuejian_07.yuejian_07.model.UserModel;

public class EnrollActActivity extends Activity implements RetrofitListener<User>,RetrofitListenerTow<ActivityOrder> {

    TextView u_name,u_phone,a_name,a_price,a_intro;
    Button insure;
    ImageView img_return;

    private int activity_id;
    private String activity_name;
    private int activity_price;
    private String activity_intro;
    private int user_id;
    private String user_name;
    private String user_phone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        setContentView(R.layout.activity_enrollact);
        initViews();
        initEvents();
        initData();
    }

    public void initViews(){
        a_name=(TextView)findViewById(R.id.act_name);
        a_price=(TextView)findViewById(R.id.act_price);
        u_name=(TextView)findViewById(R.id.user_name);
        u_phone=(TextView)findViewById(R.id.user_phone);
        a_intro=(TextView) findViewById(R.id.info);
        insure=(Button) findViewById(R.id.enroll);
        img_return=(ImageView) findViewById(R.id.img_return);
    }

    public void initEvents(){

        img_return.setOnClickListener(new View.OnClickListener() {//返回
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        insure.setOnClickListener(new View.OnClickListener(){//购买跳转
            @Override
            public void onClick(View v){
                enroll();//报名
            }
        });

    }

    public void initData(){

        getActivityInfo();
        getUserInfo();

    }

    //获取活动信息数据
    public void getActivityInfo(){
        Bundle bundle = getIntent().getExtras();
        activity_id = bundle.getInt("activity_id");
        activity_name = bundle.getString("activity_name");
        activity_price = bundle.getInt("activity_price");
        activity_intro = bundle.getString("activity_intro");
        a_name.setText(activity_name);
        a_intro.setText(activity_intro);
        a_price.setText("￥"+activity_price);
    }

    //获取个人用户信息
    public void getUserInfo(){
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id = sharedPreferences.getInt("user_id", 0);
        UserModel userModel = new UserModel();
        userModel.getUserById(user_id,this);
    }

    @Override
    public void onSuccess(User user){
        user_name = user.getUser_name();
        user_phone = user.getUser_phone();
        u_name.setText(user_name);
        u_phone.setText(user_phone);
    }
    @Override
    public void onFail(){ Toast.makeText(EnrollActActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}

    //下订单
    private void enroll() {

        ActivityModel activityModel = new ActivityModel();
        activityModel.insertOrder(user_id,activity_id,this);

    }

    @Override
    public void onSuccessTow(ActivityOrder activityOrder){
        String result = activityOrder.getSuccess();
        if (!result.equals("0")){
            Toast.makeText(EnrollActActivity.this,"报名成功！",Toast.LENGTH_SHORT).show();
            finish();
        }else{
            Toast.makeText(EnrollActActivity.this,"报名失败！",Toast.LENGTH_SHORT).show();
        }
    }
    @Override
    public void onFailTow(){ Toast.makeText(EnrollActActivity.this, "网络发生错误", Toast.LENGTH_SHORT).show();}


}
